<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #main {
        width: 800px;
        height: 500px;
        /* background-color: red; */
      }
    </style>
  </head>

  <body>
    <div id="main"></div>
    <script src="./lib/echarts.min.js"></script>
    <script>
      const arr =[
    {name:"西兰花",price:150},
    {name:"西瓜",price:230},
    {name:"西葫芦",price:224},
    {name:"西北风",price:34},
    {name:"西红柿",price:135},
    {name:"土豆",price:147},
    {name:"秋刀鱼",price:260},
]
      //遍历数组 得到数据
      const dataName =[]
      const dataPrice =[]
     arr.forEach(item =>{

       dataName.push(item.name)
       dataPrice.push(item.price)
     })
      // 获取容器
      var chartDom = document.getElementById('main')
      // 初始化图表
      var myChart = echarts.init(chartDom)
      var option

      option = {
        xAxis: {
          type: 'category',
          data:dataName
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            data:dataPrice,
            type: 'bar',
            color:['#fc8452', '#9a60b4', '#ea7ccc']
          },
        ],
        
        
      }

      option && myChart.setOption(option)
    </script>
  </body>
</html>
